@extends('layouts.application')
@section('content')
    <!-- content start -->
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">广告分类</strong> /
                    <small>Category List</small>
                </div>
            </div>

            <hr>
            @include('layouts.shared._flash')

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('ad.category.create')}}" type="button" class="am-btn am-btn-default"><span
                                        class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" class="delete_all am-btn am-btn-default"><span
                                        class="am-icon-trash-o"></span> 删除
                            </button>
                        </div>
                    </div>
                </div>
                <div class="am-u-sm-12 am-u-md-3">
                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm'}">
                            <option value="option1">所有类别</option>
                            <option value="option2">IT业界</option>
                            <option value="option3">数码产品</option>
                            <option value="option3">笔记本电脑</option>
                            <option value="option3">平板电脑</option>
                            <option value="option3">只能手机</option>
                            <option value="option3">超极本</option>
                        </select>
                    </div>
                </div>
                <div class="am-u-sm-12 am-u-md-3">
                    <div class="am-input-group am-input-group-sm">
                        <input type="text" class="am-form-field">
                        <span class="am-input-group-btn">
            <button class="am-btn am-btn-default" type="button">搜索</button>
          </span>
                    </div>
                </div>
            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-check"><input type="checkbox" class="check_all"></th>
                                <th class="table-id">编号</th>
                                <th class="table-title">缩略图</th>
                                <th class="table-title">分类名</th>
                                <th class="table-title">是否显示</th>
                                <th class="table-author am-hide-sm-only">排序</th>
                                <th class="table-set">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($categories as $category)
                                <tr data-id="{{$category->id}}">
                                    <td><input type="checkbox" value="{{$category->id}}" class="checked_id"
                                               name="checked_id[]"></td>
                                    <td>{{$category->id}}</td>
                                    <td>
                                        <img src="{{$category->thumb}}" alt="" style="width: 80px;height: 80px">

                                    </td>
                                    <td>{{$category->name}}</td>
                                    <td class="am-hide-sm-only" data-attr="">
                                        {!! is_something($category,'is_show') !!}

                                    </td>
                                    <td class="am-hide-sm-only">
                                        <input class="sort_order " type="text" value="{{$category->sort_order}}"
                                               style="width: 40px;height: 40px">

                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('ad.category.edit',$category->id)}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </a>
                                                <button class=" delete_one am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$categories->total()}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    {{$categories->links()}}
                                </ul>
                            </div>
                        </div>
                        <hr/>
                        <p>注：.....</p>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
    <!-- content end -->
@endsection
@section('js')
    <script>
        $(function () {
            //删除单个
            $('.delete_one').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this);
                $.ajax({
                    type: 'DELETE',
                    url: '/admin/ad/category/' + id,
                    data: id,
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                })
                return false;
            })

            //全选反选
            $('.check_all').click(function () {
                $(':checkbox').prop('checked', this.checked);
            })
            //删除多个
            $('.delete_all').click(function () {

                var length = $('.checked_id:checked').length;
                if (length == 0) {
                    alert('您至少选中一条！')
                }
                var checked_id = $('.checked_id:checked').serialize();
                $.ajax({
                    type: 'DELETE',
                    url: "{{route('ad.category.delete_all')}}",
                    data: checked_id,
                    success: function () {
                        $('.checked_id:checked').parents('tr').fadeOut(400);
                    }
                });
                return false;
            })
            //改变属性
            $('.change_attr').click(function () {

                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr');
                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: "{{route('ad.category.change_attr')}}",
                    data: {id: id, attr: attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close')
                    }
                })
                return false;
            })
            //排序
            $('.sort_order').change(function () {
                var id = $(this).parents('tr').data('id');
                var sort_order = $(this).val();
                $.ajax({
                    type: 'PATCH',
                    url: "{{route('ad.category.sort_order')}}",
                    data: {id: id, sort_order: sort_order},
                    success:function () {
                        location.reload();
                    }
                })
                return false;
            })
        })
    </script>
@endsection

